#include "PassThroughModel.h"

#include "core/AppDirectory.h"

namespace {
const QString UserFilename = "pcl.pass_through_model.json";
}

PassThroughModel::PassThroughModel()
    : BasePclModel()
{
    setPclHelpPage("https://pcl.readthedocs.io/projects/tutorials/en/master/passthrough.html");
}

PassThroughModel& PassThroughModel::get()
{
    static PassThroughModel instance;
    if (instance.filename().isEmpty()) {
        instance.setFilename(AppDirectory::get().getAppDocFile(UserFilename));
        instance.load();
    }
    return instance;
}

void PassThroughModel::fromJson(const QJsonObject& json, QStringList& errorList)
{

    BasePclModel::fromJson(json, errorList);

    pcdFile_ = json["pcdFile"].toString(pcdFile_);
    sampleSize_ = json["sampleSize"].toInt(sampleSize_);
    sampleMinPos_ = json["sampleMinPos"].toDouble(sampleMinPos_);
    sampleMaxPos_ = json["sampleMaxPos"].toDouble(sampleMaxPos_);
    filterFieldName_ = json["filterFieldName"].toString(filterFieldName_);
    limitMin_ = json["limitMin"].toDouble(limitMin_);
    limitMax_ = json["limitMax"].toDouble(limitMax_);
}

QJsonObject PassThroughModel::toJson() const
{
    QJsonObject json = BasePclModel::toJson();

    json.insert("pcdFile", QJsonValue(pcdFile_));
    json.insert("sampleSize", QJsonValue(sampleSize_));
    json.insert("sampleMinPos", QJsonValue(sampleMinPos_));
    json.insert("sampleMaxPos", QJsonValue(sampleMaxPos_));
    json.insert("filterFieldName", QJsonValue(filterFieldName_));
    json.insert("limitMin", QJsonValue(limitMin_));
    json.insert("limitMax", QJsonValue(limitMax_));

    return json;
}
